1499C - Minimum Grid Path - CodeForces Solution


brute force data structures greedy math *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
const int maxn=5e5+45,inf=0x3f3f3f3f;
ll idx1,idx2,a[maxn],MIN1[maxn];
ll MIN2[maxn];
ll b[maxn],c[maxn],pre1[maxn],pre2[maxn];
void work(){ 
    int n;
    idx1=idx2=0;
    ll ans=MIN1[0]=MIN2[0]=1e18;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
    	scanf("%lld",&c[i]);
//    	MIN[i]=min(MIN[i-1],c[i]);
    	if(i%2)a[++idx1]=c[i];
    	else b[++idx2]=c[i];
	}
	for(int i=1;i<=idx1;i++)pre1[i]=pre1[i-1]+a[i],MIN1[i]=min(MIN1[i-1],a[i]);
	for(int i=1;i<=idx2;i++)pre2[i]=pre2[i-1]+b[i],MIN2[i]=min(MIN2[i-1],b[i]);
	for(int i=2;i<=n;i++){
		int i1,i2;
		if(i%2)i2=i/2,i1=i2+1;
		else i1=i2=i/2;
		ans=min(ans,(n-i1)*MIN1[i1]+pre1[i1]+(n-i2)*MIN2[i2]+pre2[i2]);
//		else ans=min(ans,)
	}
	printf("%lld\n",ans);
}
int main()
{
	int t=1;
	scanf("%d",&t);
	while(t--)
	work();
	return 0;
 } 
		   		   		 		    	   	    		


Comments

Submit
0 Comments
More Questions

1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers